package com.zyf.school.manager.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zyf.school.manager.domain.Dict;
import com.zyf.school.manager.domain.DictTree;
import com.zyf.school.manager.service.DictService;
import com.zyf.school.manager.service.DictTreeService;
import com.zyf.school.manager.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/dict/")
public class DictController {

    @Autowired
    private DictTreeService dictTreeService;

    @Autowired
    private DictService dictService;
    private List<Dict> dictList;
    private Dict dict;
    private int totalCount;

    private PageBean page;

    public void init(){
        this.page = new PageBean();
        this.totalCount = dictService.count();
        page.setTotalCount(this.totalCount);
        page.setPageSize(5);
    }

    //基础参数设置
    @RequestMapping("param")
    public String param(HttpServletRequest request, @RequestParam(name = "startIndex") int startIndex, @RequestParam(name="pageSize") int pageSize){
        dictList = dictService.queryAllParent(startIndex,pageSize);
        List<Dict> dictTestList = dictService.queryByParentid(0,0,100);
        List<DictTree> list = dictTreeService.queryDictTree(0);
        request.setAttribute("dictTree",JSON.toJSON(list));
        request.setAttribute("dictList",dictList);
        request.setAttribute("allDictList",dictService.queryAll(startIndex,pageSize));
        return "/dict/param";
    }
    public void printDictList(DictTree dt){

    }

    @RequestMapping(value = "ajaxQuery",method = RequestMethod.POST)
    @ResponseBody
    public List<JSONObject> ajaxQuery(HttpServletResponse response,@RequestParam(name = "startIndex") int startIndex, @RequestParam(name="pageSize") int pageSize){
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        dictList = dictService.queryAll(startIndex,pageSize);
        JSONObject obj = null;
        StringBuffer sb = new StringBuffer();
        ArrayList<JSONObject> list = new ArrayList<>();
        for (Dict dic : dictList){
            obj = new JSONObject();
            obj.put("title",dic.getName());
            obj.put("id",dic.getId());
            List<Dict> li = dictService.queryByParentid(dic.getId(),startIndex,pageSize);
            sb.append("[");
            if (li.size()>0){
                for (int index =0; index<li.size(); index++) {
                    sb.append("{title:"+li.get(index).getName()+",id:"+li.get(index).getId()+"}");
                }
            }
            obj.put("children",sb.toString());
            list.add(obj);
        }
        return list;
    }

    @RequestMapping("insert")
    public String insertDict(HttpServletRequest request,Dict dict){
        dictService.insertDict(dict);
        return param(request,0,5);
    }

    @RequestMapping("queryById")
    public String queryById(HttpServletRequest request,@RequestParam(name = "id")int id,@RequestParam(name = "startIndex") int startIndex, @RequestParam(name="pageSize") int pageSize){
        dictList = dictService.queryByParentid(id,startIndex,pageSize);
        request.setAttribute("dictList",dictList);
        request.setAttribute("allDictList",dictService.queryAll(startIndex,pageSize));
        return "/dict/param";
    }
    @RequestMapping("table")
    public String table(HttpServletRequest request){
        init();
        request.setAttribute("allDictList",dictService.queryAll(page.getStartIndex(),page.getPageSize()));
        request.setAttribute("page",page);
        return "/dict/table";
    }
    @RequestMapping("tableTree")
    public String tableTree(HttpServletRequest request,@RequestParam(name = "id") int id){
        init();
        request.setAttribute("allDictList",dictService.queryByParentid(id,page.getStartIndex(),page.getPageSize()));
        request.setAttribute("page",page);
        return "/dict/table";
    }

    @RequestMapping("page")
    public String page(HttpServletRequest request,@RequestParam(name = "id") int id,@RequestParam(name = "currentPage") int currentPage){
        init();
        page.setCurrentPage(currentPage);
        request.setAttribute("allDictList",dictService.queryAll(page.getStartIndex(),page.getPageSize()));
        request.setAttribute("page",page);
        return "/dict/table";
    }
}
